package com.wanxi.service.Impl;

import com.wanxi.dao.Impl.NewDaoImpl;
import com.wanxi.dao.NewsDao;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.NewsModel;
import com.wanxi.service.NewsService;

/**
 * @author Li
 * @Date 2023/5/7 12:10
 */
public class NewsServiceImpl implements NewsService {
    private NewsDao newsDao = new NewDaoImpl();
    public ResultDto add(NewsModel newsModel) {
        boolean same = newsDao.findSameName(newsModel);
        if (same){
            return ResultDto.builder()
                    .code(0)
                    .msg("新闻标题已存在")
                    .build();
        }
        return ResultDto.builder()
                .code(0)
                .msg("添加成功")
                .count(newsDao.add(newsModel))
                .build();
    }

    public ResultDto update(NewsModel newsModel) {
//        boolean same = newsDao.findSameName(newsModel);
//        if (same){
//            return ResultDto.builder()
//                    .code(0)
//                    .msg("用户名已存在")
//                    .build();
//        }
        return ResultDto.builder()
                .code(0)
                .msg("修改成功")
                .count(newsDao.update(newsModel))
                .build();
    }

    public ResultDto delete(NewsModel newsModel) {
        return ResultDto.builder()
                .code(0)
                .msg("删除成功")
                .count(newsDao.delete(newsModel))
                .build();
    }

    public ResultDto findAll(NewsModel newsModel) {
        return ResultDto.builder()
                .code(0)
                .msg("查询成功")
                .count(newsDao.count(newsModel))
                .data(newsDao.findAll(newsModel))
                .build();
    }

    public ResultDto findById(NewsModel newsModel) {

        return ResultDto.builder()
                .code(0)
                .msg("查询成功")
                .data(newsDao.findById(newsModel))
                .build();
    }

    @Override
    public Integer totalCount() {
        return newsDao.totalCount();
    }
}
